package cn.stronglink.assetmanage.business.inspection.model;

import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.business.inspection.view.repairDetail.RepairDetailView;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.PatrolDetailDao;
import cn.stronglink.assetmanage.dao.RepailDetailDao;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.dao.UserDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.PatrolDetail;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.netrequest.business.patrol.PatrolService;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by zhengjinglan on 2018/6/20.
 */

public class RepairDetailModel {


    private onRepairDetailListener onRepairDetailListener;

    public onRepairDetailListener getRepairDetailListener() {
        return onRepairDetailListener;
    }

    public void setCheckDetailListener(onRepairDetailListener onRepairDetailListener) {
        this.onRepairDetailListener = onRepairDetailListener;
    }

    /**
     * 根据资产id获得资产详情
     *
     * @param sn
     * @return
     */
    public SearchAsset getAssetBySn(String sn) {
        if (sn != null && !"".equals(sn)) {
            List<SearchAsset> asset = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getSearchAssetDao().queryBuilder().where(SearchAssetDao.Properties.SerialNo.eq(sn)).
                    list();
            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
            if (asset != null && asset.size() > 0) {
                return asset.get(0);
            }
        }
        return null;
    }

    /**
     * 根据资产id获得资产详情
     *
     * @param sn
     * @return
     */
    public RepailDetail getRepailDetailBySn(String sn) {
        if (sn != null && !"".equals(sn)) {
            RepailDetail repailDetail = null;
            List<RepailDetail> repailDetailList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT)
                    .getRepailDetailDao().queryBuilder().where(RepailDetailDao.Properties.SerialNo.eq(sn)).
                            list();
            if (repailDetailList != null && repailDetailList.size() > 0) {
                repailDetail = repailDetailList.get(0);
            }
            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
            if (repailDetail != null) {
                return repailDetail;
            }
        }
        return null;
    }

    /**
     * 根据维修人名称获取该维修人的index
     *
     * @param repairName
     * @return
     */
    public long getUserNameIndex(String repairName) {
        if (repairName != null && !"".equals(repairName)) {
            User user = null;
            List<User> users = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getUserDao().
                    queryBuilder().where(UserDao.Properties.Name.eq(repairName)).list();
            if (users != null && users.size() > 0) {
                user = users.get(0);
                return user.getUser_id();
            }
        }
        return 0;
    }

    /**
     * 获取维修人列表
     *
     * @return
     */
    public List<User> getUserData() {
        List<User> userList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getUserDao().loadAll();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (userList != null) {
            return userList;
        }
        return null;
    }

    /**
     * 保存入库数据
     *
     * @param repair
     */
    public void saveRepairDetailata(Repair repair) {

        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().update(repair);
        // 获取详情
        if (repair.getDetailList() != null && repair.getDetailList().size() > 0) {
            for (RepailDetail detail : repair.getDetailList()) {
                detail.setRepairId_(repair.getId_());
                DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().insertOrReplace(detail);
            }
        }
    }


    public interface onRepairDetailListener {
        void onFailure(Call call, IOException e);

        void onResponse(Call call, Response response);
    }
}
